package com.szosen.jkzj.activity;

import java.util.ArrayList;
import java.util.List;

import a.a.a;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.szosen.application.MyApplication;
import com.szosen.database.AccessoryExamination;
import com.szosen.database.ChineseMedicineManage;
import com.szosen.database.DBService;
import com.szosen.database.Examination;
import com.szosen.database.HealthAssessment;
import com.szosen.database.HealthRecord;
import com.szosen.database.InhospitalSituation;
import com.szosen.database.LifestySituation;
import com.szosen.database.MedicineSituation;
import com.szosen.database.NonimmuneInoculation;
import com.szosen.database.OldPeopleSelfCare;
import com.szosen.database.UploadRecord;
import com.szosen.database.VisitPlan;
import com.szosen.database.Visit_info;
import com.szosen.database.Visit_info2;
import com.szosen.database.base_info;
import com.szosen.jkzj.R;
import com.szosen.jkzj.activity.ggws.Lnrshzlnl;
import com.szosen.jkzj.activity.ggws.Lnrzyygl;
import com.szosen.jkzj.activity.mbgy.GlycuresisTure;
import com.szosen.jkzj.activity.mbgy.HypertensionTure;
import com.szosen.jkzj.activity.mbgy.plan.GXYPlan_Activity;
import com.szosen.jkzj.activity.mbgy.plan.TNBPlan_Activity;
import com.szosen.json.database.ArchiveBean;
import com.szosen.json.database.JSONDBService;
import com.szosen.util.BackGroundThread;
import com.szosen.util.OfflineUploadAdapter;
import com.szosen.util.ToastTool;
import com.szosen.web.Fetch;
import com.szosen.web.ResultListener;
import com.szosen.web.UploadContant;
import com.szosen.web.WebService;
import com.szosen.xml.XmlTool;

/***
 * 离线上传上传的界面。
 * 
 * @author geshenjibi
 * 
 */
public class OfflineUploadListActivity extends BaseActivity {
	private String flag;
	private ListView mListView;
	private OfflineUploadAdapter mAdapter;
	private List<String[]> datas;
	private TextView tv_recordAll, tv_recordOK, tv_recordFailed;
	private AlertDialog dialog;
	private int count_ok = 0;
	private int count_failed = 0;
	private int count_all = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_uploadoffline);
		super.setTitleContent("【离线列表】");
		initView();
	}

	@Override
	@SuppressLint("ResourceAsColor")
	protected void onResume() {
		super.onResume();
		getDataFromDatabase();
	}

	public void initView() {
		tv_recordAll = (TextView) findViewById(R.id.uploadoffline_tv_recordAll);
		tv_recordOK = (TextView) findViewById(R.id.uploadoffline_tv_recordok);
		tv_recordFailed = (TextView) findViewById(R.id.uploadoffline_tv_recordfailed);
		datas = new ArrayList<String[]>();
		mAdapter = new OfflineUploadAdapter(context, datas);
		mListView = (ListView) findViewById(R.id.uploadoffline_listview);
		mListView.setAdapter(mAdapter);
		TextView tv_item3 = (TextView) findViewById(R.id.item_3);
		flag = getIntent().getStringExtra(BaseActivity.ACTIVITY_FLAG);
		switch (flag) {
		case BaseActivity.ACTIVITY_FLAG_JKDA:
			super.setTitleContent("【健康档案离线列表】");
			break;
		case BaseActivity.ACTIVITY_FLAG_JKTJ:
			tv_item3.setText("责任医生");
			super.setTitleContent("【健康体检离线列表】");
			break;
		case BaseActivity.ACTIVITY_FLAG_MBGY:
			tv_item3.setText("类型");
			super.setTitleContent("【慢病干预离线列表】");
			break;
		case BaseActivity.ACTIVITY_FLAG_GGWS:
			tv_item3.setText("类型");
			super.setTitleContent("【公共卫生离线列表】");
			break;
		default:
			break;
		}
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String[] item = (String[]) parent.getAdapter()
						.getItem(position);
				String data2 = item[2];// 类型
				String data3 = item[3];
				BaseActivity.setIdCard(data3);
				ArchiveBean queryAB = new ArchiveBean();
				queryAB.setIdcard(data3);
				List<ArchiveBean> abs = (List<ArchiveBean>) JSONDBService
						.queryByIdCard(queryAB);
				if (abs != null && abs.size() > 0) {
					BaseActivity.setArchiveBean(abs.get(0));
				}
				Intent intent = null;
				switch (flag) {
				case BaseActivity.ACTIVITY_FLAG_JKDA:
					intent = new Intent(OfflineUploadListActivity.this,
							JKDAActivity.class);
					intent.putExtra("time", item[5]);
					break;
				case BaseActivity.ACTIVITY_FLAG_JKTJ:
					intent = new Intent(OfflineUploadListActivity.this,
							JKTJActivity2.class);
					intent.putExtra("time", item[5]);
					break;
				case BaseActivity.ACTIVITY_FLAG_MBGY:
					if (data2.equals("高血压")) {
						intent = new Intent(OfflineUploadListActivity.this,
//								HypertensionTure.class);
						 GXYPlan_Activity.class);
					} else if (data2.equals("糖尿病")) {
						intent = new Intent(OfflineUploadListActivity.this,
//								GlycuresisTure.class);
						 TNBPlan_Activity.class);
					}
					String json = "";
					List<Object> vps = DBService.getAll(new VisitPlan(data3));
					if (vps != null && vps.size() > 0) {
						VisitPlan vp = (VisitPlan) vps.get(0);
						json = vp.getJson();
					}
					if (!TextUtils.isEmpty(json)) {
						intent.putExtra("json", json);
					}
					intent.putExtra("time", item[5]);
					break;
				case BaseActivity.ACTIVITY_FLAG_GGWS:
					if (data2.equals("自理能力")) {
						intent = new Intent(OfflineUploadListActivity.this,
								Lnrshzlnl.class);
					} else if (data2.equals("中医药")) {
						intent = new Intent(OfflineUploadListActivity.this,
								Lnrzyygl.class);
					}
					intent.putExtra("time", item[5]);
					break;
				default:
					break;
				}
				if (intent != null) {
					startActivity(intent);
				}
			}
		});
		mListView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(final AdapterView<?> parent,
					View view, final int position, long id) {
				String[] item = (String[]) parent.getAdapter()
						.getItem(position);
				final String time = item[5];
				AlertDialog.Builder builder = new AlertDialog.Builder(
						OfflineUploadListActivity.this);
				builder.setMessage("您确定要删除该离线记录？");
				builder.setPositiveButton("确定",
						new AlertDialog.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// 用时间为唯一标准查询或者插入
								UploadRecord ur = new UploadRecord();
								ur.setTime(time);
								DBService.delete(ur);
								// 重新更新数据
								getDataFromDatabase();
							}
						});
				builder.setNegativeButton("取消", null);
				builder.create().show();
				return true;
			}
		});
	}

	private static final int what_success_ok = 65;
	private static final int what_failed = 77;
	private static final int what_error = 87;
	Handler jsonHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case what_success_ok:
				// ToastTool.showLong(context, "上传成功");
				break;
			case what_failed:
				// ToastTool.showLong(context, "" + msg.obj);
				break;
			case what_error:
				// ToastTool.showLong(context, "访问出错，请检查网络或配置信息");
				break;
			}
		};
	};

	/***
	 * 上传未上传的记录
	 * 
	 * @param view
	 */
	public void uploadAll(View view) {
		if (!MyApplication.isConnected()) {
			// 如果还没网络连接，提示先网络连接。
			ToastTool.showLong(context, "当前网络未连接，请检查网络或设置！");
			return;
		}
		if (count_failed == 0) {
			ToastTool.showLong(context, "当前无需要上传的离线数据");
			return;
		}
		// AlertDialog.Builder builder = new AlertDialog.Builder(
		// OfflineUploadListActivity.this);
		// builder.setTitle("提示：");
		// builder.setMessage("离线数据正在上传");
		// builder.setNegativeButton("取消", new AlertDialog.OnClickListener() {
		//
		// @Override
		// public void onClick(DialogInterface dialog, int which) {
		// BackGroundThread.stopAll();
		// mAdapter.notifyDataSetChanged();
		// }
		// });
		// dialog = builder.create();
		// dialog.show();
		final ProgressDialog progressDialog = new ProgressDialog(
				OfflineUploadListActivity.this);
		progressDialog.setTitle("提示");
		progressDialog.setMessage("数据上传中，请稍候...");
		progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		progressDialog.setProgress(0);
		progressDialog.setButton("确定", new AlertDialog.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				BackGroundThread.stopAll();
				getDataFromDatabase();
			}
		});
		progressDialog.setMax(count_failed);
		progressDialog.show();
		switch (flag) {
		case BaseActivity.ACTIVITY_FLAG_JKDA:
			ArchiveBean aBean = new ArchiveBean();
			List<Object> lists = JSONDBService.getAll(aBean);
			for (int i = 0; i < lists.size(); i++) {
				ArchiveBean item = (ArchiveBean) lists.get(i);
				final String time = item.getTime();
				String data0 = "";
				String data4 = "";
				UploadRecord ur = DBService.getUploadRecordByTime(time);
				if (ur != null) {
					data0 = ur.getUploadState();
					data4 = ur.getUploadMessage();
					if (data0.equals(UploadContant.UPLOADOK)) {
					} else if (data0.equals(UploadContant.UPLOADFAILED)) {
						progressDialog.setProgress(progressDialog.getProgress()
								+ 1 + i);
						// 未上传的，开启线程上传数据到后台
						String jsonStr = ur.getJsonStr();
						WebService.addArchive(context, time, jsonHandler,
								jsonStr);
					}
				}
				// dialog.dismiss();
			}
			break;
		case BaseActivity.ACTIVITY_FLAG_JKTJ:
			List<Object> jktj_lists = DBService.getAll(new base_info());
			for (int i = 0; i < jktj_lists.size(); i++) {
				base_info item = (base_info) jktj_lists.get(i);
				final String time = item.getTime();
				String data0 = "";
				String data4 = "";
				UploadRecord ur = DBService.getUploadRecordByTime(time);
				if (ur != null) {
					data0 = ur.getUploadState();
					data4 = ur.getUploadMessage();
					if (data0.equals(UploadContant.UPLOADOK)) {
					} else if (data0.equals(UploadContant.UPLOADFAILED)) {
						progressDialog.setProgress(progressDialog.getProgress()
								+ 1 + i);
						String jsonStr = ur.getJsonStr();
						WebService.addHealthCheck(context, time, jsonHandler,
								jsonStr);
					}
				}
			}
			progressDialog.setProgress(progressDialog.getMax());
			break;
		case BaseActivity.ACTIVITY_FLAG_MBGY:
			List<Object> mbgy_gxy_lists = DBService.getAll(new Visit_info());
			for (int i = 0; i < mbgy_gxy_lists.size(); i++) {
				Visit_info item = (Visit_info) mbgy_gxy_lists.get(i);
				final String time = item.getTime();
				String data0 = "";
				String data4 = "";
				UploadRecord ur = DBService.getUploadRecordByTime(time);
				if (ur != null) {
					data0 = ur.getUploadState();
					data4 = ur.getUploadMessage();
					if (data0.equals(UploadContant.UPLOADOK)) {
					} else if (data0.equals(UploadContant.UPLOADFAILED)) {
						progressDialog.setProgress(progressDialog.getProgress()
								+ 1 + i);
						// 重新开始上传数据
						// 添加到数据库
						String jsonStr = ur.getJsonStr();
						WebService.addVisit(context, time, jsonHandler,
								jsonStr, ur.getType(), ur.getIdCard());
					}
				}
			}
			List<Object> mbgy_tnb_lists = DBService.getAll(new Visit_info2());
			for (int i = 0; i < mbgy_tnb_lists.size(); i++) {
				Visit_info2 item = (Visit_info2) mbgy_tnb_lists.get(i);
				final String time = item.getTime();
				String data0 = "";
				String data4 = "";
				UploadRecord ur = DBService.getUploadRecordByTime(time);
				if (ur != null) {
					data0 = ur.getUploadState();
					data4 = ur.getUploadMessage();
					if (data0.equals(UploadContant.UPLOADOK)) {
					} else if (data0.equals(UploadContant.UPLOADFAILED)) {
						String jsonStr = ur.getJsonStr();
						if (!TextUtils.isEmpty(jsonStr)) {
							WebService.addVisit(context, time, jsonHandler,
									jsonStr, ur.getType(), ur.getIdCard());
						}
					}
				}
			}
			progressDialog.setProgress(progressDialog.getMax());
			break;
		case BaseActivity.ACTIVITY_FLAG_GGWS:
			List<Object> ggws_lnrshzlnl_lists = DBService
					.getAll(new OldPeopleSelfCare());
			for (int i = 0; i < ggws_lnrshzlnl_lists.size(); i++) {
				OldPeopleSelfCare item = (OldPeopleSelfCare) ggws_lnrshzlnl_lists
						.get(i);
				final String time = item.getTime();
				String data0 = "";
				String data4 = "";
				UploadRecord ur = DBService.getUploadRecordByTime(time);
				if (ur != null) {
					data0 = ur.getUploadState();
					data4 = ur.getUploadMessage();
					if (data0.equals(UploadContant.UPLOADOK)) {
						count_ok++;
					} else if (data0.equals(UploadContant.UPLOADFAILED)) {
						progressDialog.setProgress(progressDialog.getProgress()
								+ 1 + i);
						// 重新开始上传数据
						// 添加到数据库 老年人上火自理能力
						String jsonStr = ur.getJsonStr();
						WebService.addOldAssessment(context, time, jsonHandler,
								jsonStr);
					}
				}
			}
			List<Object> ggws_lnrzyygl_lists = DBService
					.getAll(new ChineseMedicineManage());
			for (int i = 0; i < ggws_lnrzyygl_lists.size(); i++) {
				ChineseMedicineManage item = (ChineseMedicineManage) ggws_lnrzyygl_lists
						.get(i);
				final String time = item.getTime();
				String data0 = "";
				String data4 = "";
				UploadRecord ur = DBService.getUploadRecordByTime(time);
				if (ur != null) {
					data0 = ur.getUploadState();
					data4 = ur.getUploadMessage();
					if (data0.equals(UploadContant.UPLOADOK)) {
						count_ok++;
					} else if (data0.equals(UploadContant.UPLOADFAILED)) {
						progressDialog.setProgress(progressDialog.getProgress()
								+ 1 + i);
						// 添加到数据库
						String jsonStr = ur.getJsonStr();
						WebService.addZhongYiWenJuan(context, time,
								jsonHandler, jsonStr);
					}
				}
			}
			break;
		default:
			break;
		}
	}

	/***
	 * 请求服务器失败
	 */
	public void requestError(String time) {
		UploadRecord ur = new UploadRecord();
		ur.setTime(time);
		ur.setUploadState(UploadContant.UPLOADFAILED);
		ur.setUploadMessage("网络错误");
		DBService.add(ur);
	}

	public String getJKTJUploadStringByTime(String time) {
		/** 基本情况 */
		base_info mjktj_baseInfo = null;
		/** 查体 */
		Examination mjktj_Examination = null;
		/** 辅助检查 */
		AccessoryExamination mjktj_AccessoryExamination = null;
		/** 健康评价表 */
		HealthAssessment mjktj_HealthAssessment = null;
		/** 生活方式 */
		LifestySituation mjktj_LifestySituation = null;
		/** 住院用药情况 */
		MedicineSituation mjktj_MedicineSituation = null;
		/** 住院治疗情况 */
		InhospitalSituation mjktj_InhospitalSituation = null;
		/** 非免疫接种 */
		NonimmuneInoculation mjktj_NonimmuneInoculation = null;
		base_info b = new base_info();
		b.setTime(time);
		Examination e = new Examination();
		e.setTime(time);
		NonimmuneInoculation n = new NonimmuneInoculation();
		n.setTime(time);
		AccessoryExamination a = new AccessoryExamination();
		a.setTime(time);
		HealthAssessment h = new HealthAssessment();
		h.setTime(time);
		LifestySituation l = new LifestySituation();
		l.setTime(time);
		MedicineSituation m = new MedicineSituation();
		m.setTime(time);
		InhospitalSituation i = new InhospitalSituation();
		i.setTime(time);
		List<base_info> bs = (List<base_info>) DBService.queryByKey(b);
		if (bs.size() > 0) {
			mjktj_baseInfo = bs.get(0);
		}
		/** 查体 */
		List<Examination> es = (List<Examination>) DBService.queryByKey(e);
		if (es.size() > 0) {
			mjktj_Examination = es.get(0);
		}
		List<NonimmuneInoculation> ns = (List<NonimmuneInoculation>) DBService
				.queryByKey(n);
		if (ns.size() > 0) {
			mjktj_NonimmuneInoculation = ns.get(0);
		}
		List<AccessoryExamination> as = (List<AccessoryExamination>) DBService
				.queryByKey(a);
		if (as.size() > 0) {
			mjktj_AccessoryExamination = as.get(0);
		}
		List<HealthAssessment> hs = (List<HealthAssessment>) DBService
				.queryByKey(h);
		if (hs.size() > 0) {
			mjktj_HealthAssessment = hs.get(0);
		}
		List<LifestySituation> ls = (List<LifestySituation>) DBService
				.queryByKey(l);
		if (ls.size() > 0) {
			mjktj_LifestySituation = ls.get(0);
		}
		List<MedicineSituation> ms = (List<MedicineSituation>) DBService
				.queryByKey(m);
		if (ms.size() > 0) {
			mjktj_MedicineSituation = ms.get(0);
		}
		List<InhospitalSituation> is = (List<InhospitalSituation>) DBService
				.queryByKey(i);
		if (is.size() > 0) {
			mjktj_InhospitalSituation = is.get(0);
		}
		mjktj_baseInfo = (mjktj_baseInfo == null ? new base_info()
				: mjktj_baseInfo);
		/** 查体 */
		mjktj_Examination = (mjktj_Examination == null ? new Examination()
				: mjktj_Examination);
		/** 非免疫接种 */
		mjktj_NonimmuneInoculation = (mjktj_NonimmuneInoculation == null ? new NonimmuneInoculation()
				: mjktj_NonimmuneInoculation);
		/** 辅助检查 */
		mjktj_AccessoryExamination = (mjktj_AccessoryExamination == null ? new AccessoryExamination()
				: mjktj_AccessoryExamination);
		/** 健康评价表 */
		mjktj_HealthAssessment = (mjktj_HealthAssessment == null ? new HealthAssessment()
				: mjktj_HealthAssessment);
		/** 生活方式 */
		mjktj_LifestySituation = (mjktj_LifestySituation == null ? new LifestySituation()
				: mjktj_LifestySituation);
		/** 住院用药情况 */
		mjktj_MedicineSituation = (mjktj_MedicineSituation == null ? new MedicineSituation()
				: mjktj_MedicineSituation);
		/** 住院治疗情况 */
		mjktj_InhospitalSituation = (mjktj_InhospitalSituation == null ? new InhospitalSituation()
				: mjktj_InhospitalSituation);
		String uploadStr = XmlTool.getJktj2String(mjktj_baseInfo,
				mjktj_LifestySituation, mjktj_Examination,
				mjktj_AccessoryExamination, mjktj_InhospitalSituation,
				mjktj_MedicineSituation, mjktj_NonimmuneInoculation,
				mjktj_HealthAssessment);
		return uploadStr;
	}

	public void checkResult(String response, String time) {
		if (!TextUtils.isEmpty(response)) {
			UploadRecord ur = new UploadRecord();
			ur.setTime(time);
			// 如果成功了。
			if (response.contains("success")) {
				ur.setUploadState(UploadContant.UPLOADOK);
				int start = response.indexOf("<returntext>");
				start += 12;
				int end = response.indexOf("</returntext>");
				if (start >= 0 && end >= 0 && start < end) {
					String data = response.substring(start, end);
					ur.setUploadMessage(data);
				} else {
					ur.setUploadMessage("");
				}
			} else if (response.contains("error")) {
				// 未上传
				ur.setUploadState(UploadContant.UPLOADFAILED);
				// <returntext>上传成功！</returntext>
				int start = response.indexOf("<returntext>");
				start += 12;
				int end = response.indexOf("</returntext>");
				if (start >= 0 && end >= 0 && start < end) {
					String data = response.substring(start, end);
					ur.setUploadMessage(data);
				} else {
					ur.setUploadMessage("");
				}
			}
			DBService.add(ur);
		}
	}

	public void deleteUploadok(View view) {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				OfflineUploadListActivity.this);
		builder.setTitle("提示：");
		builder.setMessage("正在清除已上传数据");
		builder.setNegativeButton("确定", new AlertDialog.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
				dialog.dismiss();
			}
		});
		dialog = builder.create();
		dialog.show();
		switch (flag) {
		case BaseActivity.ACTIVITY_FLAG_JKDA:
			List<Object> lists = DBService.getAll(new HealthRecord());
			for (int i = 0; i < lists.size(); i++) {
				HealthRecord item = (HealthRecord) lists.get(i);
				final String time = item.getTime();
				String data0 = "";
				UploadRecord ur = DBService.getUploadRecordByTime(time);
				if (ur != null) {
					data0 = ur.getUploadState();
					if (data0.equals(UploadContant.UPLOADOK)) {
						DBService.delete(ur);
					}
				}
			}
			break;
		case BaseActivity.ACTIVITY_FLAG_JKTJ:
			List<Object> jktj_lists = DBService.getAll(new base_info());
			for (int i = 0; i < jktj_lists.size(); i++) {
				base_info item = (base_info) jktj_lists.get(i);
				final String time = item.getTime();
				String data0 = "";
				UploadRecord ur = DBService.getUploadRecordByTime(time);
				if (ur != null) {
					data0 = ur.getUploadState();
					if (data0.equals(UploadContant.UPLOADOK)) {
						DBService.delete(ur);
					}
				}
			}
			break;
		case BaseActivity.ACTIVITY_FLAG_MBGY:
			List<Object> mbgy_gxy_lists = DBService.getAll(new Visit_info());
			for (int i = 0; i < mbgy_gxy_lists.size(); i++) {
				Visit_info item = (Visit_info) mbgy_gxy_lists.get(i);
				final String time = item.getTime();
				String data0 = "";
				UploadRecord ur = DBService.getUploadRecordByTime(time);
				if (ur != null) {
					data0 = ur.getUploadState();
					if (data0.equals(UploadContant.UPLOADOK)) {
						DBService.delete(ur);
					}
				}
			}
			List<Object> mbgy_tnb_lists = DBService.getAll(new Visit_info2());
			for (int i = 0; i < mbgy_tnb_lists.size(); i++) {
				Visit_info2 item = (Visit_info2) mbgy_tnb_lists.get(i);
				final String time = item.getTime();
				String data0 = "";
				UploadRecord ur = DBService.getUploadRecordByTime(time);
				if (ur != null) {
					data0 = ur.getUploadState();
					if (data0.equals(UploadContant.UPLOADOK)) {
						DBService.delete(ur);
					}
				}
			}
			break;
		case BaseActivity.ACTIVITY_FLAG_GGWS:
			List<Object> ggws_lnrshzlnl_lists = DBService
					.getAll(new OldPeopleSelfCare());
			for (int i = 0; i < ggws_lnrshzlnl_lists.size(); i++) {
				OldPeopleSelfCare item = (OldPeopleSelfCare) ggws_lnrshzlnl_lists
						.get(i);
				final String time = item.getTime();
				String data0 = "";
				UploadRecord ur = DBService.getUploadRecordByTime(time);
				if (ur != null) {
					data0 = ur.getUploadState();
					if (data0.equals(UploadContant.UPLOADOK)) {
						DBService.delete(ur);
					}
				}
			}
			List<Object> ggws_lnrzyygl_lists = DBService
					.getAll(new ChineseMedicineManage());
			for (int i = 0; i < ggws_lnrzyygl_lists.size(); i++) {
				ChineseMedicineManage item = (ChineseMedicineManage) ggws_lnrzyygl_lists
						.get(i);
				final String time = item.getTime();
				String data0 = "";
				UploadRecord ur = DBService.getUploadRecordByTime(time);
				if (ur != null) {
					data0 = ur.getUploadState();
					if (data0.equals(UploadContant.UPLOADOK)) {
						DBService.delete(ur);
					}
				}
			}
			break;
		default:
			break;
		}
		dialog.dismiss();
		getDataFromDatabase();
	}

	/***
	 * 从数据库获取数据然后填充到listView中
	 */
	public void getDataFromDatabase() {
		datas.clear();
		count_ok = 0;
		count_failed = 0;
		count_all = 0;
		switch (flag) {
		case BaseActivity.ACTIVITY_FLAG_JKDA:
			ArchiveBean aBean = new ArchiveBean();
			List<Object> lists = JSONDBService.getAll(aBean);
			for (int i = 0; i < lists.size(); i++) {
				ArchiveBean item = (ArchiveBean) lists.get(i);
				String time = item.getTime();
				String data0 = "";
				String data4 = "";
				UploadRecord ur = DBService.getUploadRecordByTime(time);
				if (ur != null) {
					data0 = ur.getUploadState();
					data4 = ur.getUploadMessage();
					if (data0.equals(UploadContant.UPLOADOK)) {
						count_ok++;
					} else if (data0.equals(UploadContant.UPLOADFAILED)) {
						count_failed++;
					}
					String data1 = item.getBirthday();
					String data2 = item.getName();
					String data3 = item.getIdcard();
					String data5 = time;
					String[] data = new String[] { data0, data1, data2, data3,
							data4, data5 };
					datas.add(data);
				}
			}
			count_all = count_failed + count_ok;
			break;
		case BaseActivity.ACTIVITY_FLAG_JKTJ:
			List<Object> jktj_lists = DBService.getAll(new base_info());
			for (int i = 0; i < jktj_lists.size(); i++) {
				base_info item = (base_info) jktj_lists.get(i);
				String time = item.getTime();
				String data0 = "";
				String data4 = "";
				UploadRecord ur = DBService.getUploadRecordByTime(time);
				if (ur != null) {
					data0 = ur.getUploadState();
					data4 = ur.getUploadMessage();
					if (data0.equals(UploadContant.UPLOADOK)) {
						count_ok++;
					} else if (data0.equals(UploadContant.UPLOADFAILED)) {
						count_failed++;
					}
					String data1 = item.getCheckDate();
					String data2 = item.getDoctorName();
					String data3 = item.getIdCard();
					String data5 = time;
					String[] data = new String[] { data0, data1, data2, data3,
							data4, data5 };
					datas.add(data);
				}
			}
			count_all = count_failed + count_ok;
			break;
		case BaseActivity.ACTIVITY_FLAG_MBGY:
			List<Object> mbgy_gxy_lists = DBService.getAll(new Visit_info());
			for (int i = 0; i < mbgy_gxy_lists.size(); i++) {
				Visit_info item = (Visit_info) mbgy_gxy_lists.get(i);
				String time = item.getTime();
				String data0 = "";
				String data4 = "";
				UploadRecord ur = DBService.getUploadRecordByTime(time);
				if (ur != null) {
					data0 = ur.getUploadState();
					data4 = ur.getUploadMessage();
					if (data0.equals(UploadContant.UPLOADOK)) {
						count_ok++;
					} else if (data0.equals(UploadContant.UPLOADFAILED)) {
						count_failed++;
					}
					String data1 = item.getVisitDate();// 类型 高血压、糖尿病
					String data2 = "高血压";//
					String data3 = ur.getIdCard();
					String data5 = time;
					String[] data = new String[] { data0, data1, data2, data3,
							data4, data5 };
					datas.add(data);
				}
			}
			List<Object> mbgy_tnb_lists = DBService.getAll(new Visit_info2());
			for (int i = 0; i < mbgy_tnb_lists.size(); i++) {
				Visit_info2 item = (Visit_info2) mbgy_tnb_lists.get(i);
				String time = item.getTime();
				String data0 = "";
				String data4 = "";
				UploadRecord ur = DBService.getUploadRecordByTime(time);
				if (ur != null) {
					data0 = ur.getUploadState();
					data4 = ur.getUploadMessage();
					if (data0.equals(UploadContant.UPLOADOK)) {
						count_ok++;
					} else if (data0.equals(UploadContant.UPLOADFAILED)) {
						count_failed++;
					}
					String data1 = item.getVisitDate();// 类型 高血压、糖尿病
					String data2 = "糖尿病";// 随访日期
					String data3 = ur.getIdCard();
					String data5 = time;
					String[] data = new String[] { data0, data1, data2, data3,
							data4, data5 };
					datas.add(data);
				}
			}
			count_all = count_failed + count_ok;
			break;
		case BaseActivity.ACTIVITY_FLAG_GGWS:
			List<Object> ggws_lnrshzlnl_lists = DBService
					.getAll(new OldPeopleSelfCare());
			for (int i = 0; i < ggws_lnrshzlnl_lists.size(); i++) {
				OldPeopleSelfCare item = (OldPeopleSelfCare) ggws_lnrshzlnl_lists
						.get(i);
				String time = item.getTime();
				String data0 = "";
				String data4 = "";
				UploadRecord ur = DBService.getUploadRecordByTime(time);
				if (ur != null) {
					data0 = ur.getUploadState();
					data4 = ur.getUploadMessage();
					if (data0.equals(UploadContant.UPLOADOK)) {
						count_ok++;
					} else if (data0.equals(UploadContant.UPLOADFAILED)) {
						count_failed++;
					}
					String data1 = item.getInputDate();//
					String data2 = "自理能力";// 类型
					String data3 = item.getIdCard();
					String data5 = time;
					String[] data = new String[] { data0, data1, data2, data3,
							data4, data5 };
					datas.add(data);
				}
			}
			List<Object> ggws_lnrzyygl_lists = DBService
					.getAll(new ChineseMedicineManage());
			for (int i = 0; i < ggws_lnrzyygl_lists.size(); i++) {
				ChineseMedicineManage item = (ChineseMedicineManage) ggws_lnrzyygl_lists
						.get(i);
				String time = item.getTime();
				String data0 = "";
				String data4 = "";
				UploadRecord ur = DBService.getUploadRecordByTime(time);
				if (ur != null) {
					data0 = ur.getUploadState();
					data4 = ur.getUploadMessage();
					if (data0.equals(UploadContant.UPLOADOK)) {
						count_ok++;
					} else if (data0.equals(UploadContant.UPLOADFAILED)) {
						count_failed++;
					}
					String data1 = item.getReportDate();// 建表日期
					String data2 = "中医药";// 类型
					String data3 = item.getIdCard();
					String data5 = time;
					String[] data = new String[] { data0, data1, data2, data3,
							data4, data5 };
					datas.add(data);
				}
			}
			count_all = count_failed + count_ok;
			break;
		default:
			break;
		}
		tv_recordAll.setText(count_all + "");
		tv_recordFailed.setText(count_failed + "");
		tv_recordOK.setText(count_ok + "");
		mAdapter.notifyDataSetChanged();
	}
}
